<?php
if (!defined('APP_ID')) { die('No direct script access allowed'); }

class Database_Query_Mysql_Insert extends Database_Query_Mysql {
	
	private $_table = '';
	private $_fields = array();

	public function table($table) {
		$this->_table = $table;
		return $this;
	}

	public function field($field, $value, $type = null) {
		$elem = array();
		$elem['field'] = $field;
		$elem['value'] = $value;
		$elem['type'] = $type;
		$this->_fields[$field] = $elem;
		return $this;
	}
	
	public function data($data) {
		foreach ($data as $item) {
			$this->field($item['field'], $item['value'], $item['type']);
		}
		return $this;
	}

	public function compile() {
		$sql = "INSERT INTO `" . $this->_table . "` (";

		$sep = false;
		foreach ($this->_fields as $field) {
			if ($sep) { $sql .= ","; }
			$sql .= "`" . $field['field'] . "`";
			$sep = true;
		}
		
		$sql .= ") VALUES (";
		
		$sep = false;
		foreach ($this->_fields as $field) {
			if ($sep) { $sql .= ","; }

			$sql .= $this->value($field['value'], $field['type']);
			$sep = true;
		}
		$sql .= ")";
		
		return $sql;
	}
	
}
